package com.kj.xuyuan.util;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

/**
 * 算是重写了个吐司,优化误操作多的时候疯狂弹吐司的情况。
 */
public class ToastUtil {

    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            if (mToast != null)
                mToast.cancel();
            mToast = null;
        }
    };

    public static void showToast(Context mContext, String text) {
        mHandler.removeCallbacks(r);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            if (mContext != null)
                mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
        }
        mHandler.postDelayed(r, 3000);
        if (mToast != null)
            mToast.show();
    }

    public static void showDataErrorToast(Context mContext) {
        mHandler.removeCallbacks(r);
        if (mToast != null) {
            mToast.setText("数据错误");
        } else {
            if (mContext != null)
                mToast = Toast.makeText(mContext, "数据错误", Toast.LENGTH_SHORT);
        }
        mHandler.postDelayed(r, 3000);
        if (mToast != null)
            mToast.show();
    }

}